from pathlib import Path
from flask import Flask, render_template, request, jsonify
import locale

app = Flask(__name__)
locale.setlocale(locale.LC_ALL, 'zh_CN.UTF-8')


@app.route("/")
def index():
    # 检查A盘到Z盘列出存在的盘符
    drivers = []
    for i in range(ord('A'), ord('Z') + 1):
        path = Path(chr(i) + ':\\')
        if path.exists():
            drivers.append(path)

    return render_template("drivers.html", drivers=drivers)


@app.route("/files")
def files():
    dir = request.args.get('dir', '')
    path = Path(dir)
    if not path.exists():
        return "404 路径不存在"
    else:
        files = path.iterdir()
        dir_list = []
        file_list = []
        for file in files:
            if file.is_dir():
                dir_list.append(file)
            else:
                file_list.append(file)
        # 渲染并返回 "files.html" 模板
        return render_template("files.html", dir_list=dir_list, file_list=file_list)


if __name__ == "__main__":
    app.run(host='0.0.0.0', port=5000, debug=True, threaded=False)
